package com.slot.faraohgold;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class FaraohGoldView extends SurfaceView implements SurfaceHolder.Callback {
	class FaraohGoldThread extends Thread {
		private SurfaceHolder mSurfaceHolder;
		
		private Context mContext;
		private int isimbols=23;
		private Bitmap[] mSimbols=new Bitmap[isimbols];
		private Bitmap mBGbitmap;
		private Bitmap mBPanel;
		private int[] y=new int[5];
		private float scaleX;
		private float scaleY;
		private float bpDeltaY=380;
		private Paint mPaint= new Paint();
		
		
		
		public FaraohGoldThread (SurfaceHolder surfaceHolder, Context context,
                Handler handler) {
			mSurfaceHolder = surfaceHolder;
            //mHandler = handler;
            //mContext = context;
            mSimbols[0]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim0);
            mSimbols[1]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim1);
            mSimbols[2]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim2);
            mSimbols[3]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim3);
            mSimbols[4]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim4);
            mSimbols[5]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim5);
            mSimbols[6]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim6);
            mSimbols[7]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim7);
            mSimbols[8]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim8);
            mSimbols[9]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim9);
            mSimbols[10]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim0);
            mSimbols[11]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim1);
            mSimbols[12]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim2);
            mSimbols[13]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim3);
            mSimbols[14]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim4);
            mSimbols[15]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim5);
            mSimbols[16]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim6);
            mSimbols[17]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim7);
            mSimbols[18]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim8);
            mSimbols[19]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim9);
            mSimbols[20]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim0);
            mSimbols[21]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim1);
            mSimbols[22]=BitmapFactory.decodeResource(context.getResources(), R.drawable.sim2);
            mBGbitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.tpanel);
            mBPanel = BitmapFactory.decodeResource(context.getResources(), R.drawable.bpanel);
            mPaint.setAlpha(200);
            
		}
		
		@Override
        public void run() {
			drawFrame();
			mHandler.postDelayed(mDrawFrame, 1000 / 40);
        }
		private final Runnable mDrawFrame = new Runnable() {
	        public void run() {
	        	if(sMode!="game ready") drawFrame();
	        	if(sMode=="panel ready"&&!button0.isShown()) {
	        		button0.setVisibility(View.VISIBLE);
	        		button0.setWidth((int) (50*scaleX));
	        		//button0.setMargins(0,0,0,0);
	        	} else if(sMode!="panel ready"&&button0.isShown()) button0.setVisibility(View.INVISIBLE);
	            mHandler.removeCallbacks(mDrawFrame);
	            mHandler.postDelayed(mDrawFrame, 1000 / 40);
	        }
	    };
	    private final Runnable mChangeStatus = new Runnable() {
	        public void run() {
	        	if(sMode=="reel anim") sMode="game ready";
	        	y= new int[]{0,0,0,0,0};
	            mHandler.removeCallbacks(mChangeStatus);
	            
	        }
	    };
		public void drawFrame(){
			Canvas c = null;
            try {
                c = mSurfaceHolder.lockCanvas(null);
                synchronized (mSurfaceHolder) {
                    doDraw(c);
                }
            } finally {
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);

                }
            }
            
		}
		public void setSurfaceSize(int width, int height) {
            // synchronized to make sure these all change atomically
            synchronized (mSurfaceHolder) {

            	scaleX = (float) width/640;
                scaleY = (float) height/480;
            	mBGbitmap = Bitmap.createScaledBitmap(
            			mBGbitmap, width, height, true);
            	mBPanel = Bitmap.createScaledBitmap(
            			mBPanel, width, (int) (209 * scaleY), true);
            	for(int i=0;i<isimbols;i++){
                	mSimbols[i]=Bitmap.createScaledBitmap(
                			mSimbols[i],(int)(100*scaleX),(int)(100*scaleY), true);
        		}
                Log.v(">>>>>>","scaleY="+scaleY+" scaleX="+scaleX);
            }
        }
		private void doDraw(Canvas canvas) {
			//canvas.save();
			canvas.drawColor(Color.WHITE);
			if(sMode=="reel anim"){
				if(y[0]<(int)(100*scaleY*(isimbols-3)-50*scaleY)) y[0]+=65*scaleY; else y[0]=(int) (100*scaleY*(isimbols-3));
				if(y[1]<(int)(100*scaleY*(isimbols-3)-50*scaleY)) y[1]+=60*scaleY; else y[1]=(int) (100*scaleY*(isimbols-3));
				if(y[2]<(int)(100*scaleY*(isimbols-3)-50*scaleY)) y[2]+=55*scaleY; else y[2]=(int) (100*scaleY*(isimbols-3));
				if(y[3]<(int)(100*scaleY*(isimbols-3)-50*scaleY)) y[3]+=50*scaleY; else y[3]=(int) (100*scaleY*(isimbols-3));
				if(y[4]<(int)(100*scaleY*(isimbols-3)-50*scaleY)) y[4]+=45*scaleY; else {
					y[4]=(int) (100*scaleY*(isimbols-3));
					//sMode="game ready";
				}
			}
	        for(int i=0;i<isimbols;i++){
	        	canvas.drawBitmap(mSimbols[i],scaleX*41,y[0]-i*100*scaleY+270*scaleY, null);
	        	canvas.drawBitmap(mSimbols[i],scaleX*155,y[1]-i*100*scaleY+270*scaleY, null);
	        	canvas.drawBitmap(mSimbols[i],scaleX*269,y[2]-i*100*scaleY+270*scaleY, null);
	        	canvas.drawBitmap(mSimbols[i],scaleX*383,y[3]-i*100*scaleY+270*scaleY, null);
	        	canvas.drawBitmap(mSimbols[i],scaleX*497,y[4]-i*100*scaleY+270*scaleY, null);
	        }
	        canvas.drawBitmap(mBGbitmap,0,0, null);
	        if(sMode=="panel shift+") {
	        	if(bpDeltaY<380) bpDeltaY+=20; else sMode="game ready";
	        } else if(sMode=="panel shift-") {
	        	if(bpDeltaY>280) bpDeltaY-=20; else {
	        		sMode="panel ready";
	        	}
	        }
	        if(sMode=="panel ready") canvas.drawBitmap(mBPanel,0,280* scaleY, mPaint);
	        else canvas.drawBitmap(mBPanel,0,bpDeltaY* scaleY, null);
	        //canvas.restore();
		}
	}
	
	private FaraohGoldThread thread;
	public String sMode;
	public Handler mHandler=new Handler();
	float tempY = 0;
	private Button button0;
	
	public FaraohGoldView(Context context, AttributeSet attrs) {
		super(context, attrs);
		SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        sMode="init";
        
        thread = new FaraohGoldThread(holder,context,null);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int action = event.getAction();
		
		 
        if (action == MotionEvent.ACTION_DOWN){
        	if(sMode=="game ready"&&event.getY()<380*thread.scaleY){ 
        		sMode="reel anim";
        		mHandler.postDelayed(thread.mChangeStatus, 3500L);
        	}
        	tempY=event.getY();
        } else if(action == MotionEvent.ACTION_MOVE){
        	if(sMode=="game ready"&&event.getY()>380*thread.scaleY&&event.getY()<tempY){
         		sMode="panel shift-";
         	} else if(sMode=="panel ready"&&event.getY()<350*thread.scaleY&&event.getY()>tempY){
         		sMode="panel shift+";
         	}
        }
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
		// TODO Auto-generated method stub
		thread.setSurfaceSize(width, height);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		sMode="game ready";
		thread.start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		thread.stop();
	}
	
	public void SetButtonView(Button _button) {
        button0 = _button;
        //  mButtonRestart = _buttonRestart;
    }

}

